package com.xjscrm.console.service.customer.group.impl;

import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.enums.RedisCacheKeyEnum;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.console.param.operatePlan.AddFanGroupDto;
import com.xjscrm.console.param.operatePlan.BatchSetAddFanGroupDto;
import com.xjscrm.console.param.operatePlan.FindAddFanGroupDto;
import com.xjscrm.console.param.operatePlan.SetAddFanGroupDto;
import com.xjscrm.console.service.customer.group.AddFanGroupService;
import com.xjscrm.console.vo.AddFanGroupVo;
import com.xjscrm.console.vo.FindAddFanGroupVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AddFanGroupServiceImpl implements AddFanGroupService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void setAddFanGroup(SetAddFanGroupDto setAddFanGroupDto) {
        if(setAddFanGroupDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(setAddFanGroupDto.getDeviceUniqueId() == null || StringUtils.isBlank(setAddFanGroupDto.getGroupPullKey())  || setAddFanGroupDto.getGroupIndex() == null){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "方案ID和机器人以及进粉群下标都不能为空");
        }

        String groupIndex = String.format(RedisCacheKeyEnum.GROUP_PULL_ORDER_INDEX.getKey(), setAddFanGroupDto.getGroupPullKey(), setAddFanGroupDto.getDeviceUniqueId());

        redisTemplate.opsForValue().set(groupIndex, setAddFanGroupDto.getGroupIndex());

    }


    @Override
    public void batchSetAddFanGroup(BatchSetAddFanGroupDto batchSetAddFanGroupDto) {
        List<SetAddFanGroupDto> setAddFanGroupDtoList = batchSetAddFanGroupDto.getSetAddFanGroupDtoList();
        if(setAddFanGroupDtoList == null || setAddFanGroupDtoList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "请输入批量设置列表");
        }

        for (SetAddFanGroupDto setAddFanGroupDto : setAddFanGroupDtoList) {
            setAddFanGroup(setAddFanGroupDto);
        }
    }

    @Override
    public Object findAddFanGroup(FindAddFanGroupDto findAddFanGroupDto) {
        FindAddFanGroupVo findAddFanGroupVo = new FindAddFanGroupVo();
        List<AddFanGroupVo> addFanGroupVoList = new ArrayList<>();

        List<AddFanGroupDto> addFanGroupDtoList = findAddFanGroupDto.getAddFanGroupDtoList();
        if(addFanGroupDtoList == null || addFanGroupDtoList.size() == 0){
            throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }

        for (AddFanGroupDto addFanGroupDto : addFanGroupDtoList) {
            if(addFanGroupDto.getDeviceUniqueId() == null || StringUtils.isBlank(addFanGroupDto.getGroupPullKey())){
                throw  new BusinessException(ResponseCode.PARAMETER_EXCEPTION, "方案ID和机器人都不能为空");
            }

            AddFanGroupVo addFanGroupVo = new AddFanGroupVo();
            addFanGroupVo.setDeviceUniqueId(addFanGroupDto.getDeviceUniqueId());
            addFanGroupVo.setGroupPullKey(addFanGroupDto.getGroupPullKey());

            //获取进粉群下标
            String groupIndex = String.format(RedisCacheKeyEnum.GROUP_PULL_ORDER_INDEX.getKey(), addFanGroupDto.getGroupPullKey(), addFanGroupDto.getDeviceUniqueId());
            Object object = redisTemplate.opsForValue().get(groupIndex);
            if(object == null){
                addFanGroupVo.setGroupIndex(0);
            }else{
                addFanGroupVo.setGroupIndex(Integer.parseInt(object.toString()));
            }
            addFanGroupVoList.add(addFanGroupVo);
        }

        findAddFanGroupVo.setAddFanGroupList(addFanGroupVoList);

        return findAddFanGroupVo;
    }
}
